﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlDieuChinhDonHangDaIn : System.Web.UI.UserControl
{
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    SqlTransaction tran;
    SqlCommand UPDATE_TRANNO, INSERT_ARReleaseInvoiceTracking;
    SqlConnection con;
    BCDataCenter.DataAccessLayer.DAL dal;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropTTPP();
            dal = new BCDataCenter.DataAccessLayer.DAL();
            DataSet ds = dal.MyExecuteQuery(ref error, "GET_ARPASS_1", CommandType.StoredProcedure, new SqlParameter("@Sitecode", DropTTPP.SelectedValue.ToString()));
            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
        }
    }
    void bindDropTTPP()
    {
        dal = new BCDataCenter.DataAccessLayer.DAL();
        DataTable db = dal.get_data_table("GET_SITENAME_SITECODE");
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            dal = new BCDataCenter.DataAccessLayer.DAL();
            DataSet ds = dal.MyExecuteQuery(ref error, "GET_ARPASS_1", CommandType.StoredProcedure, new SqlParameter("@Sitecode", DropTTPP.SelectedValue.ToString()));
            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            con = new SqlConnection(str);
            con.Open();
            tran = con.BeginTransaction();
            UPDATE_TRANNO = new SqlCommand();
            UPDATE_TRANNO.Connection = con;

            INSERT_ARReleaseInvoiceTracking = new SqlCommand();
            INSERT_ARReleaseInvoiceTracking.Connection = con;
            INSERT_ARReleaseInvoiceTracking.Transaction = tran;
            try
            {
                UPDATE_TRANNO.CommandText = "UPDATE_TRANNO";
                UPDATE_TRANNO.CommandType = CommandType.StoredProcedure;
                UPDATE_TRANNO.Parameters.Add(new SqlParameter("@tranno", grv.Cells[0].Text));
                UPDATE_TRANNO.Transaction = tran;
                UPDATE_TRANNO.ExecuteNonQuery();

                INSERT_ARReleaseInvoiceTracking.CommandText = "INSERT_ARReleaseInvoiceTracking";
                INSERT_ARReleaseInvoiceTracking.CommandType = CommandType.StoredProcedure;
                INSERT_ARReleaseInvoiceTracking.Parameters.Add(new SqlParameter("@Tranno", grv.Cells[0].Text));
                INSERT_ARReleaseInvoiceTracking.Parameters.Add(new SqlParameter("@Rectime", DateTime.Now.ToString()));
                INSERT_ARReleaseInvoiceTracking.Parameters.Add(new SqlParameter("@RecUser", Session["username"].ToString()));
                INSERT_ARReleaseInvoiceTracking.Transaction = tran;
                INSERT_ARReleaseInvoiceTracking.ExecuteNonQuery();

                tran.Commit();
                uscMsgBox1.AddMessage("Điều chỉnh đơn hàng số hóa đơn " + grv.Cells[0].Text + " thành công", uscMsgBox.enmMessageType.Success);
                dal = new BCDataCenter.DataAccessLayer.DAL();
                DataSet ds = dal.MyExecuteQuery(ref error, "GET_ARPASS_1", CommandType.StoredProcedure, new SqlParameter("@Sitecode", DropTTPP.SelectedValue.ToString()));
                this.GridView1.DataSource = ds.Tables[0];
                this.GridView1.DataBind();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
            }
            finally
            {
                con.Close();
            }
        }
       
    }
    protected void DropTTPP_SelectedIndexChanged(object sender, EventArgs e)
    {
        dal = new BCDataCenter.DataAccessLayer.DAL();
        DataSet ds = dal.MyExecuteQuery(ref error, "GET_ARPASS_1", CommandType.StoredProcedure, new SqlParameter("@Sitecode", DropTTPP.SelectedValue.ToString()));
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();
    }
   
}
